package com.sgb.contract.view.ui.contract;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.sgb.contract.BR;
import com.sgb.contract.R;
import com.sgb.contract.base.BaseActivity;
import com.sgb.contract.databinding.ContractActivityContractBinding;
import com.sgb.contract.model.ContractCategoryBean;
import com.sgb.contract.utils.ArouterConfigs;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * 个人中心-合同
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.11.04
 */
@Route(path = ArouterConfigs.AR_CONTRACT)
public class ContractActivity extends BaseActivity<ContractActivityContractBinding, ContractModel> {

    public static void start(Context context) {
        Intent intent = new Intent(context, ContractActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.contract_activity_contract;
    }

    @Override
    protected void initViewModel() {
        mViewModel = createViewModel(ContractModel.class, BR.contractModel);
    }


    @Override
    protected void init() {
        mDataBinding.head.contractTitle.setText(R.string.name_title_contract);
        mViewModel.getContractCategory();
    }

    @Override
    protected void initListener() {
        mDataBinding.head.contractIvBack.setOnClickListener(v -> finish());
    }

    @Override
    protected void initObserve() {
        mViewModel.contractCategory.observe(ContractActivity.this, contractCategoryBeans -> {
            if (null == contractCategoryBeans) {
                mViewModel.showNoData(mDataBinding.contentContainer);
                return;
            }
            mDataBinding.contractViewPager.setAdapter(new FragmentStateAdapter(this) {
                @Override
                public int getItemCount() {
                    return contractCategoryBeans.size();
                }

                @NotNull
                @Override
                public Fragment createFragment(int position) {
                    return ContractItemFragment.newInstance(ContractActivity.this, contractCategoryBeans.get(position));
                }
            });

            TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(mDataBinding.contractTabType, mDataBinding.contractViewPager, (tab, position) -> tab.setText(contractCategoryBeans.get(position).getValue()));
            tabLayoutMediator.attach();

            for (int i = 0; i < mDataBinding.contractTabType.getTabCount(); i++) {
                TabLayout.Tab tab = mDataBinding.contractTabType.getTabAt(i);
                if (tab != null) {
                    tab.setCustomView(getTabView(i, contractCategoryBeans));
                }
            }
            mDataBinding.contractTabType.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    updateTabTextView(tab, true);
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    updateTabTextView(tab, false);
                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });
            updateTabTextView(mDataBinding.contractTabType.getTabAt(mDataBinding.contractTabType.getSelectedTabPosition()), true);
        });
    }

    @SuppressLint("InflateParams")
    private View getTabView(int currentPosition, List<ContractCategoryBean> table) {
        View view = LayoutInflater.from(this).inflate(R.layout.contract_tab_item_textview, null);
        TextView textView = view.findViewById(R.id.contract_tv_tab_item_textview);
        textView.setTextColor(ContextCompat.getColor(this, R.color.color_ff666666));
        textView.setText(table.get(currentPosition).getValue());
        return view;
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        if (tab == null || tab.getCustomView() == null) {
            return;
        }
        if (isSelect) {
            TextView tabSelect = tab.getCustomView().findViewById(R.id.contract_tv_tab_item_textview);
            tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tabSelect.setTextColor(ContextCompat.getColor(this, R.color.contract_color_EF4033));
            tabSelect.setText(tab.getText());
        } else {
            TextView tabUnSelect = tab.getCustomView().findViewById(R.id.contract_tv_tab_item_textview);
            tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tabUnSelect.setTextColor(ContextCompat.getColor(this, R.color.color_ff666666));
            tabUnSelect.setText(tab.getText());
        }
    }
}
